﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Drawing;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class TicketsController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH = "~/Content/images/posts";

        //
        // GET: /Tickets/

        public ViewResult Index()
        {
            Post post = db.Posts.Where(p => p.Key == "Tickets").SingleOrDefault();

            if (post == null)
            {
                post = new Post()
                {
                    Key = "Tickets",
                    TitleEN = "Tickets",
                    TitleFR = "Billets",
                    ContentEN = "Tickets details",
                    ContentFR = "Détails des billets",
                    UserCreatedBy = ViewBag.CurrentUser,
                    DateCreated = DateTime.Now,
                    UserModifiedBy = ViewBag.CurrentUser,
                    DateModified = DateTime.Now
                };

                db.Posts.Attach(post);
                db.Posts.Add(post);
                db.SaveChanges();
            }

            return View(post);
        }

        //
        // GET: /Tickets/Edit/5
 
        public ActionResult Edit()
        {
            Post post = db.Posts.Where(p => p.Key == "Tickets").Single();
            return PartialView(post);
        }

        //
        // POST: /Tickets/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Post post, HttpPostedFileBase inputFile, string nopicture)
        {
            if (nopicture == "on")
            {
                post.Picture = null;
            }
            else if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, post.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    post.Picture = filename;
                }
            }

            Post postdb = db.Posts.Find(post.Id);

            post.UserModifiedBy = ViewBag.CurrentUser;
            post.DateModified = DateTime.Now;
            
            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");

            TryUpdateModel(postdb);

            if (ModelState.IsValid)
            {
                postdb.Picture = post.Picture; // Quickfix...
                db.Posts.Attach(postdb);
                db.Entry(postdb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(post);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}